import {
  DEFAULT_PROVIDER,
  STORAGE_PROVIDER_KEY,
  SESSION_MAXHEIGHT_KEY,
  STORAGE_EXECER_KEY,
  SESSION_MAXHASH_KEY,
  DEFAULT_EXECER
} from '@/config/contants.js'
// 默认从localStroge获取
export function getValueInLocal(storageKey, defaultValue) {
  const storaged = localStorage.getItem(storageKey)
  if (storaged && storaged !== 'undefined') return storaged
  return defaultValue
}

// 默认从Session获取
export function getValueInSession(storageKey, defaultValue) {
  const storaged = sessionStorage.getItem(storageKey)
  if (storaged && storaged !== 'undefined') return storaged
  return defaultValue
}

export const defaultNodeAddress = getValueInLocal(STORAGE_PROVIDER_KEY, DEFAULT_PROVIDER)

const state = {
  // 节点最大高度
  maxHeight: Number(getValueInSession(SESSION_MAXHEIGHT_KEY, 0)),
  // 节点最大高度哈希
  maxHash: getValueInSession(SESSION_MAXHASH_KEY, ''),
  // 与节点的连接状态
  connectStatus: true,
  // 节点配置
  apiSetting: {
    // 节点地址
    provider: defaultNodeAddress,
    // 执行器
    execer: getValueInLocal(STORAGE_EXECER_KEY, process.env.VUE_APP_DEFAULT_EXECER || DEFAULT_EXECER)
  },
  //  线上Token列表
  tokenList: [],
  // token列表加载成功
  tokenListSuccess: false,
  // 主代币
  baseCoin: process.env.VUE_APP_BASECOIN || 'BTY',
  // 主代币图标
  baseCoinIcon: require(`../assets/img/coin/${process.env.VUE_APP_BASECOIN_URL}`),
  // 初始语言
  initLang: localStorage.getItem("BLOCK_LANG") || process.env.VUE_APP_DEFAULT_LANG
};

export default state;